package org.ruiyichen.guizhou.agr.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.base.BaseActivity;
import org.ruiyichen.guizhou.agr.bean.Response;
import org.ruiyichen.guizhou.agr.config.Constant;
import org.ruiyichen.guizhou.agr.net.OkHttpManager;
import org.ruiyichen.guizhou.agr.ui.mineview.BToast;
import org.ruiyichen.guizhou.agr.utils.StrUtil;

import java.util.HashMap;
import java.util.Map;

public class IrrigatedSystemActivity extends BaseActivity implements View.OnClickListener {

    private Button btn1,btn2,btn3,btn4;
    private TextView tv1, tv2, tv3;
    private EditText et1, et2;
    private ImageView view;
    private AnimationDrawable animationDrawable;//声明AnimationDrawable类


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_irrigated_system);
        bOkHttpManager = new OkHttpManager(this, bHandler);
        tv1 = findViewById(R.id.tv_1);
        tv2 = findViewById(R.id.tv_2);
        tv3 = findViewById(R.id.tv_3);
        initViews();
//        tv1.setText("20");
//        tv2.setText("15");
//        initData();


    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    protected void initViews() {
        //高水位
        et1 = findViewById(R.id.et_1);
        //低水位
        et2 = findViewById(R.id.et_2);
        view = findViewById(R.id.view);

        // 通过逐帧动画的资源文件获得AnimationDrawable示例
        animationDrawable=(AnimationDrawable) getResources().getDrawable(R.drawable.image_load_anim);

    }

    @Override
    protected void initEvents() {
        findViewById(R.id.btn_1).setOnClickListener(this);
        findViewById(R.id.btn_2).setOnClickListener(this);
        findViewById(R.id.btn_3).setOnClickListener(this);
        findViewById(R.id.btn_4).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_1:
                //全自动模式
                if (StrUtil.isEmpty(et1.getText())) {
                    BToast.getInstance().show("请输入高水位~");
                    return;
                }
                else if (StrUtil.isEmpty(et2.getText()))
                {
                    BToast.getInstance().show("请输入低水位~");
                    return;
                }else if ( StrUtil.compare(et2.getText().toString() , et1.getText().toString()))
                {
                    BToast.getInstance().show("高水位不能小于低水位！");
                    return;
                }

                submit1();
                BToast.getInstance().show("已切换至全自动模式");
                tv3.setText("全自动模式");
                break;
            case R.id.btn_2:
                //蝶阀漫灌
                submit2();
                BToast.getInstance().show("已切换至蝶阀漫灌模式");
                tv3.setText("蝶阀漫灌");
                break;
            case R.id.btn_3:
                //闸阀漫灌
                submit3();
                BToast.getInstance().show("已切换至闸阀漫灌模式");
                tv3.setText("闸阀漫灌");
                break;
            case R.id.btn_4:
                //排水
                submit4();
                BToast.getInstance().show("已切换至排水模式");
                tv3.setText("排水模式");
                break;
        }
    }
//
//    private void initData() {
//        Map<String, String> body = new HashMap<>();
//        bOkHttpManager.httpRequest(Constant.RequestType.SEND_MESSAGE, Constant.Http.POST, body, false, new
//                TypeToken<Response<String, Void>>() {
//                }.getType(), true, false);
//    }


    /**
     * 全自动模式
     */
    private void submit1() {
        //http://121.199.45.128/prod-api/app/login?username=test&password=123456
        Map<String, String> body = new HashMap<>();
        body.put("mode", "1");
        body.put("highLevel", et1.getText().toString());
        body.put("lowLevel", et2.getText().toString());
        bOkHttpManager.httpRequest(Constant.RequestType.BOOTH_MODE, Constant.Http.GET, body,
                false, new TypeToken<Response<String, Void>>() {
                }.getType(), true, false);
        // 把AnimationDrawable设置为ImageView的背景
        view.setBackgroundDrawable(animationDrawable);
        if (animationDrawable != null && !animationDrawable.isRunning()) {//判断
            animationDrawable.start();
        }
    }

    /**
     * 蝶阀漫灌
     */
    private void submit2() {
        //http://121.199.45.128/prod-api/app/login?username=test&password=123456
        Map<String, String> body = new HashMap<>();
        body.put("mode", "2");
        body.put("highLevel", "");
        body.put("lowLevel", "");
        bOkHttpManager.httpRequest(Constant.RequestType.BOOTH_MODE, Constant.Http.GET, body,
                false, new TypeToken<Response<String, Void>>() {
                }.getType(), true, false);
    }


    /**
     * 闸阀漫灌
     */
    private void submit3() {
        //http://121.199.45.128/prod-api/app/login?username=test&password=123456
        Map<String, String> body = new HashMap<>();
        body.put("mode", "3");
        body.put("highLevel", "");
        body.put("lowLevel", "");
        bOkHttpManager.httpRequest(Constant.RequestType.BOOTH_MODE, Constant.Http.GET, body,
                false, new TypeToken<Response<String, Void>>() {
                }.getType(), true, false);
    }


    /**
     * 排水
     */
    private void submit4() {
        //http://121.199.45.128/prod-api/app/login?username=test&password=123456
        Map<String, String> body = new HashMap<>();
        body.put("mode", "4");
        body.put("highLevel", "");
        body.put("lowLevel", "");
        bOkHttpManager.httpRequest(Constant.RequestType.BOOTH_MODE, Constant.Http.GET, body,
                false, new TypeToken<Response<String, Void>>() {
                }.getType(), true, false);

        if (animationDrawable != null && animationDrawable.isRunning()) {
            animationDrawable.stop();
        }
    }


    @Override
    protected void onPostHandle(int requestType, Object response, boolean error, int errorCode, Object obj1, Object obj2) {


    }
}
